<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>实际工作中代码优化 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/48.f4e4e774.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/21.5c50aa1c.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/27.204436be.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/41.ced85449.js"><link rel="prefetch" href="/assets/js/42.72739213.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/47.3df1bfdb.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>工作问题解决</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/项目问题解决/vuevite.html" class="sidebar-link">vue3+vite用proxy解决跨域及proxy原理解析</a></li><li><a href="/posts/项目问题解决/大文件.html" class="sidebar-link">大文件切片上传和断点续传</a></li><li><a href="/posts/项目问题解决/代码优化.html" class="active sidebar-link">实际工作中代码优化</a></li><li><a href="/posts/项目问题解决/组件问题.html" class="sidebar-link">解决组件传值问题</a></li><li><a href="/posts/项目问题解决/动态组件.html" class="sidebar-link">动态组件实践</a></li><li><a href="/posts/项目问题解决/大屏.html" class="sidebar-link">大屏九宫格视频</a></li><li><a href="/posts/项目问题解决/滚动.html" class="sidebar-link">Vue中Element表单定位滚动到校验错误位置</a></li><li><a href="/posts/项目问题解决/红绿灯.html" class="sidebar-link">Promise实现动态红绿灯切换</a></li><li><a href="/posts/项目问题解决/懒加载.html" class="sidebar-link">实现元素懒加载</a></li><li><a href="/posts/项目问题解决/小程序组件.html" class="sidebar-link">微信小程序封装组件</a></li><li><a href="/posts/项目问题解决/axios.html" class="sidebar-link">项目中对axios的整体封装</a></li><li><a href="/posts/项目问题解决/docker.html" class="sidebar-link">docker实践(附windows安装下载教程)</a></li><li><a href="/posts/项目问题解决/eltable.html" class="sidebar-link">el-table自定义表头及无数据状态</a></li><li><a href="/posts/项目问题解决/gojs.html" class="sidebar-link">gojs入门学习</a></li><li><a href="/posts/项目问题解决/react踩坑.html" class="sidebar-link">React踩坑</a></li><li><a href="/posts/项目问题解决/小程序踩坑.html" class="sidebar-link">小程序踩坑</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="实际工作中代码优化"><a href="#实际工作中代码优化" class="header-anchor">#</a> 实际工作中代码优化</h1> <p>项目中的一个sinkCom.vue需要切换多个table表格，发现代码全都写到一个面中长达上千行，通过用动态组件的方法讲每个table页面拆分到不同的vue文件中，再讲公用的编辑和删除功能提出出来，如果后续增加新的table会更加的方便</p> <h6 id="使用componet动态渲染组件"><a href="#使用componet动态渲染组件" class="header-anchor">#</a> 使用componet动态渲染组件</h6> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span>template<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;secpage-con&quot;</span> style<span class="token operator">=</span><span class="token string">&quot;overflow-y: auto&quot;</span> id<span class="token operator">=</span><span class="token string">&quot;top-to-sinks-pane&quot;</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;mec-form-contain&quot;</span><span class="token operator">&gt;</span>
            <span class="token operator">&lt;</span>component
                <span class="token operator">:</span>is<span class="token operator">=</span><span class="token string">&quot;tbType&quot;</span>
                <span class="token operator">:</span>tbType<span class="token operator">=</span><span class="token string">&quot;tbType&quot;</span>
                <span class="token operator">:</span>projectId<span class="token operator">=</span><span class="token string">&quot;projectId&quot;</span>
                @toPage<span class="token operator">=</span><span class="token string">&quot;toPage&quot;</span>
                <span class="token operator">:</span>dataSource<span class="token operator">=</span><span class="token string">&quot;dataSource&quot;</span>
                @deleteData<span class="token operator">=</span><span class="token string">&quot;deleteData&quot;</span>
                @editData<span class="token operator">=</span><span class="token string">&quot;editData&quot;</span>
            <span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>component<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>template<span class="token operator">&gt;</span>
</code></pre></div><p>这里的tbType会动态切换成kafka-sink、hbase-sink、jdbc-sink，通过组件名的切换来达到切换table的效果</p> <p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f925d3e9dbdd40608edec109e20f5ca6~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p>引入组件
tbType为'hbase-sink'对应 HbaseSink</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> KafkaSink <span class="token keyword">from</span> <span class="token string">&quot;../table/sink/KafkaTable.vue&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> HbaseSink <span class="token keyword">from</span> <span class="token string">&quot;../table/sink/HBaseTable.vue&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> JdbcSink <span class="token keyword">from</span> <span class="token string">&quot;../table/sink/JdbcTable.vue&quot;</span><span class="token punctuation">;</span>
</code></pre></div><p>注册组件</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token literal-property property">components</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    KafkaSink<span class="token punctuation">,</span>
    JdbcSink<span class="token punctuation">,</span>
    HbaseSink<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//例子</span>
<span class="token operator">&lt;</span>component
    <span class="token comment">//:is=&quot;kafka-sink&quot;</span>
    <span class="token operator">:</span>is<span class="token operator">=</span><span class="token string">&quot;tbType&quot;</span>
    <span class="token operator">...</span>
<span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>component<span class="token operator">&gt;</span>
</code></pre></div><p>component会根据is的内容显示组件，我们用切换tbType来达到动态组件的效果</p> <h5 id="独立数据查询方法"><a href="#独立数据查询方法" class="header-anchor">#</a> 独立数据查询方法</h5> <p>每个表格数据可能不同，所以获取表格的数据独立在每个table的vue文件中,但是数据统一命名，比如tableData，currentPage等后续可以抽离出方法在父组件中封装成统一的方法进行调用，方便很多</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">paging</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token literal-property property">tableData</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token literal-property property">currentPage</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token literal-property property">count</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
            <span class="token literal-property property">pageSize</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment">//获取表格数据</span>
<span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> vm <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    vm<span class="token punctuation">.</span>loading <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    http<span class="token punctuation">.</span><span class="token function">doRequest</span><span class="token punctuation">(</span><span class="token string">&quot;yoururl&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">currentPage</span><span class="token operator">:</span> vm<span class="token punctuation">.</span>paging<span class="token punctuation">.</span>currentPage<span class="token punctuation">,</span>
        <span class="token literal-property property">pageSize</span><span class="token operator">:</span> vm<span class="token punctuation">.</span>paging<span class="token punctuation">.</span>pageSize<span class="token punctuation">,</span>
        <span class="token literal-property property">projectId</span><span class="token operator">:</span> vm<span class="token punctuation">.</span>projectId<span class="token punctuation">,</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> vm<span class="token punctuation">.</span>kafkaName<span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        vm<span class="token punctuation">.</span>loading <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> res <span class="token operator">=</span> result<span class="token punctuation">.</span>data<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>code <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            vm<span class="token punctuation">.</span>paging<span class="token punctuation">.</span>tableData <span class="token operator">=</span> res<span class="token punctuation">.</span>data
            <span class="token comment">//...</span>
        <span class="token punctuation">}</span>
   <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h5 id="组件的渲染和销毁"><a href="#组件的渲染和销毁" class="header-anchor">#</a> 组件的渲染和销毁</h5> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">vebOn</span><span class="token punctuation">(</span><span class="token string">&quot;getFetchData&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">vedData</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token function">beforeDestroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 销毁</span>
    <span class="token function">vebOff</span><span class="token punctuation">(</span><span class="token string">&quot;getFetchData&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><h5 id="抽离-编辑-和-删除-功能"><a href="#抽离-编辑-和-删除-功能" class="header-anchor">#</a> 抽离 编辑 和 删除 功能</h5> <p>每个表格都有编辑的方法，所以统一抽出来在父组件中进行调用，在函数中判断不同的子组件触发变化icon</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//sinkCom.vue</span>
<span class="token operator">&lt;</span>component
    <span class="token operator">...</span>
    @editData<span class="token operator">=</span><span class="token string">&quot;editData&quot;</span>
<span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>component<span class="token operator">&gt;</span>

<span class="token function">editData</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token punctuation">,</span> type</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'editData'</span><span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">toPage</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>name<span class="token punctuation">,</span> type<span class="token punctuation">,</span> data<span class="token punctuation">.</span>id<span class="token punctuation">,</span> data<span class="token punctuation">.</span>foldId<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment">//根据不同组件触发同一个编辑函数</span>
<span class="token function">toPage</span><span class="token punctuation">(</span><span class="token parameter">label<span class="token punctuation">,</span> comPage<span class="token punctuation">,</span> id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> icon <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">CsKafka</span><span class="token operator">:</span> <span class="token string">&quot;icon-kafka&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">CsWebhook</span><span class="token operator">:</span> <span class="token string">&quot;icon-webhook&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">CsHBase</span><span class="token operator">:</span> <span class="token string">&quot;icon-hbase&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">CsKudu</span><span class="token operator">:</span> <span class="token string">&quot;icon-kudu&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">CsJdbc</span><span class="token operator">:</span> <span class="token string">&quot;icon-jdbc&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">SinkFlinkDDL</span><span class="token operator">:</span> <span class="token string">&quot;icon-flinkddl&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> tab <span class="token operator">=</span> <span class="token punctuation">{</span>
        label<span class="token punctuation">,</span>
        comPage<span class="token punctuation">,</span>
        id<span class="token punctuation">,</span>
        <span class="token literal-property property">icon</span><span class="token operator">:</span> icon<span class="token punctuation">[</span>comPage<span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">&quot;ADD_TAB&quot;</span><span class="token punctuation">,</span> tab<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token comment">//kafka.vue</span>
<span class="token operator">&lt;</span>el<span class="token operator">-</span>tooltip
    <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;table-tooltip&quot;</span>
    <span class="token operator">:</span>open<span class="token operator">-</span>delay<span class="token operator">=</span><span class="token string">&quot;1000&quot;</span>
    <span class="token operator">:</span>content<span class="token operator">=</span><span class="token string">&quot;'编辑'&quot;</span>
    placement<span class="token operator">=</span><span class="token string">&quot;top&quot;</span>
<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>div
        <span class="token keyword">class</span><span class="token operator">=</span>&quot;
            table<span class="token operator">-</span>icon<span class="token operator">-</span>con
            flex<span class="token operator">-</span>vertical<span class="token operator">-</span>center
        &quot;
        @click<span class="token punctuation">.</span>stop<span class="token operator">=</span>&quot;
            <span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">'editData'</span><span class="token punctuation">,</span>scope<span class="token punctuation">.</span>row<span class="token punctuation">,</span> <span class="token string">'CsKafka'</span><span class="token punctuation">)</span>
        &quot;
    <span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span
            <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;table-icon icon-edit&quot;</span>
        <span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span<span class="token operator">&gt;</span>编辑<span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>el<span class="token operator">-</span>tooltip<span class="token operator">&gt;</span>
</code></pre></div><h5 id="抽离页码和当前页变化方法"><a href="#抽离页码和当前页变化方法" class="header-anchor">#</a> 抽离页码和当前页变化方法</h5> <p>这里就是为什么之前我们要统一子组件中的tableData等方法的命名，在页码变化成功的时候直接调用组件的fetchData来重新获取数据，<code>SizeChange</code>和<code>CurrentChange</code>中做的调用和判断就会更加简洁，</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//kafka.vue</span>
<span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;pane-pager&quot;</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>el<span class="token operator">-</span>pagination
        @size<span class="token operator">-</span>change<span class="token operator">=</span><span class="token string">&quot;handleSizeChange&quot;</span>
        @current<span class="token operator">-</span>change<span class="token operator">=</span><span class="token string">&quot;handleCurrentChange&quot;</span>
        <span class="token operator">:</span>current<span class="token operator">-</span>page<span class="token operator">=</span><span class="token string">&quot;paging.currentPage&quot;</span>
        <span class="token operator">:</span>page<span class="token operator">-</span>sizes<span class="token operator">=</span><span class="token string">&quot;[10, 20, 30, 40]&quot;</span>
        <span class="token operator">:</span>page<span class="token operator">-</span>size<span class="token operator">=</span><span class="token string">&quot;10&quot;</span>
        layout<span class="token operator">=</span><span class="token string">&quot;total, sizes, prev, pager, next, jumper&quot;</span>
        <span class="token operator">:</span>total<span class="token operator">=</span><span class="token string">&quot;paging.count&quot;</span>
    <span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>el<span class="token operator">-</span>pagination<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token function">handleSizeChange</span><span class="token punctuation">(</span><span class="token parameter">size</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">'SizeChange'</span><span class="token punctuation">,</span><span class="token string">'kafka-sink'</span><span class="token punctuation">,</span>size<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token function">handleCurrentChange</span><span class="token punctuation">(</span><span class="token parameter">current</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">'CurrentChange'</span><span class="token punctuation">,</span><span class="token string">'kafka-sink'</span><span class="token punctuation">,</span>current<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//sinkCon.vue</span>
<span class="token operator">&lt;</span>component
    <span class="token operator">...</span><span class="token punctuation">.</span>
    @SizeChange<span class="token operator">=</span><span class="token string">&quot;SizeChange&quot;</span>
    @CurrentChange<span class="token operator">=</span><span class="token string">&quot;CurrentChange&quot;</span>
<span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>component<span class="token operator">&gt;</span>
<span class="token comment">//这里的ref指的是组件的命名，在引入组件注册那一步声明的</span>
<span class="token function">SizeChange</span><span class="token punctuation">(</span><span class="token parameter">ref<span class="token punctuation">,</span>size</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'sizechange'</span><span class="token punctuation">,</span>ref<span class="token punctuation">,</span>size<span class="token punctuation">)</span>
    <span class="token comment">//this.$refs['kafka-sink'].paging.currentPage = 1;//变量具体</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span>ref<span class="token punctuation">]</span><span class="token punctuation">.</span>paging<span class="token punctuation">.</span>currentPage <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span>ref<span class="token punctuation">]</span><span class="token punctuation">.</span>paging<span class="token punctuation">.</span>pageSize <span class="token operator">=</span> size<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span>ref<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token function">CurrentChange</span><span class="token punctuation">(</span><span class="token parameter">ref<span class="token punctuation">,</span>size</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'currentchange'</span><span class="token punctuation">,</span>ref<span class="token punctuation">,</span>size<span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span>ref<span class="token punctuation">]</span><span class="token punctuation">.</span>paging<span class="token punctuation">.</span>currentPage <span class="token operator">=</span> size<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span>ref<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4d077477f39743faae48ced9b9fbb695~tplv-k3u1fbpfcp-watermark.image?" alt="bbb.gif"></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 10:56:12</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/项目问题解决/大文件.html" class="prev">
        大文件切片上传和断点续传
      </a></span> <span class="next"><a href="/posts/项目问题解决/组件问题.html">
        解决组件传值问题
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/48.f4e4e774.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
